﻿/*
*Frances.Shih
*2012
*scripts
*/
/*slider*/
function slider(opt) {
    var self = this;
    this.count = 0;
    this.maxsize = 0;
    this.step = 0;
    this.dir = opt.dir;
    this.interval = opt.interval;
    this.handler = opt.handler;
    this.area = opt.area;
    this.prevbtn = opt.prevbtn;
    this.nxtbtn = opt.nxtbtn;

    if (!!self.interval) {
        /*自动滚*/
        switch (self.dir) {
            case 'top': $(self.handler).find('li').removeClass('active').eq(0).addClass('active');
                setInterval(function () {
                    self.autoslideTop()
                }, self.interval);
                break;
            case 'left': $(self.handler).find('li').removeClass('active').eq(0).addClass('active');
                self.init();
                setInterval(function () {
                    self.autoslideLeft()
                }, self.interval);
                break;
        }
    }

    if (!!self.prevbtn && !!self.nxtbtn) {
        /*手动滚*/
        switch (self.dir) {
            case 'top': $(self.prevbtn).hide();
                self.count = $(self.area).find('li').eq(0).height();
                self.maxsize = $(self.area).find('li').size();
                if (self.maxsize == 1) {
                    $(self.nxtbtn).hide();
                }
                $(self.prevbtn).click(function () {
                    if (--self.step > 0) {
                        $(self.nxtbtn).show();
                    } else {
                        $(self.prevbtn).hide();
                    }
                    self.slideTop();
                });
                $(self.nxtbtn).click(function () {
                    if (++self.step < self.maxsize - 1) {
                        $(self.prevbtn).show();
                    } else {
                        $(self.nxtbtn).hide();
                    }
                    self.slideTop();
                });
                break;
            case 'left': $(self.prevbtn).hide();
                self.count = $(self.area).find('li').eq(0).width();
                self.maxsize = $(self.area).find('li').size();
                self.init();
                if (self.maxsize == 1) {
                    $(self.nxtbtn).hide();
                }
                $(self.prevbtn).click(function () {
                    if (--self.step > 0) {
                        $(self.nxtbtn).show();
                    } else {
                        $(self.prevbtn).hide();
                    }
                    self.slideLeft();
                });
                $(self.nxtbtn).click(function () {
                    if (++self.step < self.maxsize - 1) {
                        $(self.prevbtn).show();
                    } else {
                        $(self.nxtbtn).hide();
                    }
                    self.slideLeft();
                });
                break;
        }
    }

}
slider.prototype.slideTop = function () {
    var self = this;
    $(self.area).animate({
        marginTop: -self.count * self.step + 'px'
    }, "slow");
    return this;
}
slider.prototype.slideLeft = function () {
    var self = this;
    $(self.area).animate({
        marginLeft: -self.count * self.step + 'px'
    }, "slow");
    return this;
}
slider.prototype.init = function () {
    var self = this;
    self.count = $(self.area).find('li').eq(0).width();
    self.maxsize = $(self.area).find('li').size();
    $(self.area).css({ 'width': self.count * self.maxsize + 'px' });
    $(self.area).find('li').css({ 'float': 'left' });
    return self;
}

slider.prototype.autoslideTop = function () {
    var self = this;
    self.count = $(self.area).find('li').eq(0).height();
    self.maxsize = $(self.area).find('li').size();

    if (self.step < self.maxsize) {
        $(self.area).animate({
            marginTop: -self.count * self.step + 'px'
        }, "slow");
        $(self.handler).find('li').removeClass('active').eq(self.step).addClass('active');
        self.step++;
    } else {
        self.step = 0;
        $(self.area).animate({
            marginTop: -self.count * self.step + 'px'
        }, "slow");
        $(self.handler).find('li').removeClass('active').eq(self.step).addClass('active');
    }
    return self;
}

slider.prototype.autoslideLeft = function () {
    var self = this;
    self.count = $(self.area).find('li').eq(0).width();
    self.maxsize = $(self.area).find('li').size();
    if (self.step < self.maxsize) {
        $(self.area).animate({
            marginLeft: -self.count * self.step + 'px'
        }, "slow");
        $(self.handler).find('li').removeClass('active').eq(self.step).addClass('active');
        self.step++;
    } else {
        self.step = 0;
        $(self.area).animate({
            marginLeft: -self.count * self.step + 'px'
        }, "slow");
        $(self.handler).find('li').removeClass('active').eq(self.step).addClass('active');
    }
    return self;
}

/*hover*/
function Toggle(opt) {
    var self = this;
    this.handler = opt.handler,
		  this.area = opt.area;

    this.init();
}
Toggle.prototype.init = function () {
    var self = this;
    $(self.handler).hover(function () {
        $(this).find(self.area).animate({
            height: '26px'
        }, "fast");
    }, function () {
        $(this).find(self.area).animate({
            height: '0'
        }, "fast");
    });
    return self;
}

/*省事联动*/
function PC(opt) {
    var self = this;
    this.prc = opt.prc;
    this.city = opt.city;
    this.count = 1;
    this.count2 = 1;
    this.getProvince();
}
PC.prototype.getProvince = function () {
    var self = this;

    $.ajax({
        //url: '/_assets/js/pc.js',
        url: '/ajax/province.aspx',
        dataType: 'json',
        error: function () {
            alert('省市加载失败,请您稍后再试！');
        },
        success: function (json) {
            var html = "<ul>";
            for (var i = 0; i < json.length; i++) {
                html += "<li data-value=" + json[i].Id + ">" + json[i].ProvinceName + "</li>";
            }
            html += "</ul>";
            $(self.prc).append(html);
            $(self.prc).click(function () {
                var _this = $(this),
					_height = $(this).find("ul").height() + 17;
                if (self.count % 2 == 0) {
                    _this.animate({
                        height: '17px'
                    }, "slow", function () {
                        $(self.city).show();
                    });
                    self.count++;
                } else {
                    _this.animate({
                        height: _height + 'px'
                    }, "slow", function () {
                        $(self.city).hide();
                    });
                    self.count++;
                }
            });

            $(self.prc).find('li').click(function () {
                var _this = $(this);
                $(self.prc).find('span').html(_this.text()).attr("data-value", _this.attr("data-value"));
                $('input[name="province"]').val(_this.attr("data-value"));
                //self.getCity(json, _this.text());
                self.getCity(json, _this.data().value);
            });

            $(self.city).click(function () {
                var _this = $(this),
					  _height = $(this).find("ul").height() + 17;
                if (self.count2 % 2 == 0) {
                    _this.animate({
                        height: '17px'
                    }, "slow");
                    self.count2++;
                } else {
                    _this.animate({
                        height: _height + 'px'
                    }, "slow");
                    self.count2++;
                }
            });

            $(self.city).find('li').live("click", function () {
                var _this = $(this);
                $(self.city).find('span').html(_this.text()).attr("data-value", _this.attr("data-value"));
                $('input[name="city"]').val(_this.attr("data-value"));
            });
        }
    });

    return self;
}

PC.prototype.getCity = function (data, index) {
    var self = this;
    $.ajax({
        url: '/ajax/city.aspx?pid=' + index,
        dataType: 'json',
        error: function () {
            alert('省市加载失败,请您稍后再试！');
        },
        success: function (cityJson) {
            //alert(cityJson);
            $(self.city).find('span').html("您所在的城市").attr("data-value", "all");
            var html = "<ul>";
            for (var j = 0; j < cityJson.length; j++) {
                html += "<li data-value=" + cityJson[j].Id + ">" + cityJson[j].CityName + "</li>";
            }
            html += "</ul>";
            $(self.city + ' ul').remove();
            $(self.city).append(html);
        }
    });
    //    for (var i = data.length; i--; ) {
    //        if (data[i].t == index) {
    //            for (var j = 0; j < data[i].s.length; j++) {
    //                html += "<li data-value=" + data[i].s[j].v + ">" + data[i].s[j].t + "</li>";
    //            }
    //        }
    //    }
    return self;
}
/*省事联动2*/
function PC2(opt) {
    var self = this;
    this.prc2 = opt.prc2;
    this.city2 = opt.city2;
    this.count = 1;
    this.count2 = 1;
    this.getProvince();
}
PC2.prototype.getProvince = function () {
    var self = this;

    $.ajax({
        //url: '/_assets/js/pc.js',
        url: '/ajax/province.aspx',
        dataType: 'json',
        error: function () {
            alert('省市加载失败,请您稍后再试！');
        },
        success: function (json) {
            var html = "<ul>";
            for (var i = 0; i < json.length; i++) {
                html += "<li data-value=" + json[i].Id + ">" + json[i].ProvinceName + "</li>";
            }
            html += "</ul>";
            $(self.prc2).append(html);
            $(self.prc2).click(function () {
                var _this = $(this),
					_height = $(this).find("ul").height() + 24;
                if (self.count % 2 == 0) {
                    _this.css({
                        height: '24px'
                    }, "slow", function () {
                        $(self.city2).show();
                    });
                    self.count++;
                } else {
                    _this.css({
                        height: _height + 'px'
                    }, "slow");
                    self.count++;
                }
            });

            $(self.prc2).find('li').click(function () {
                var _this = $(this);
                $(self.prc2).find('span').html(_this.text()).attr("data-value", _this.attr("data-value"));
                $('input[name="province2"]').val(_this.attr("data-value"));
                //self.getCity(json, _this.text());
                self.getCity(json, _this.data().value);
            });

            $(self.city2).click(function () {
                var _this = $(this),
					  _height = $(this).find("ul").height() + 24;
                if (self.count2 % 2 == 0) {
                    _this.css({
                        height: '24px'
                    }, "slow");
                    self.count2++;
                } else {
                    _this.css({
                        height: _height + 'px'
                    }, "slow");
                    self.count2++;
                }
            });

            $(self.city2).find('li').live("click", function () {
                var _this = $(this);
                $(self.city2).find('span').html(_this.text()).attr("data-value", _this.attr("data-value"));
                $('input[name="city2"]').val(_this.attr("data-value"));
            });
        }
    });

    return self;
}

PC2.prototype.getCity = function (data, index) {
    var self = this;
    $.ajax({
        url: '/ajax/city.aspx?pid=' + index,
        dataType: 'json',
        error: function () {
            alert('省市加载失败,请您稍后再试！');
        },
        success: function (cityJson) {
            $(self.city2).find('span').html("城市").attr("data-value", "all");
            var html = "<ul>";
            for (var j = 0; j < cityJson.length; j++) {
                html += "<li data-value=" + cityJson[j].Id + ">" + cityJson[j].CityName + "</li>";
            }
            html += "</ul>";
            $(self.city2 + ' ul').remove();
            $(self.city2).append(html);
        }
    });
    //    for (var i = data.length; i--; ) {
    //        if (data[i].t == index) {
    //            for (var j = 0; j < data[i].s.length; j++) {
    //                html += "<li data-value=" + data[i].s[j].v + ">" + data[i].s[j].t + "</li>";
    //            }
    //        }
    //    }
    return self;
}

/*图片点击切换*/
function ptp(opt) {
    var self = this;
    this.handler = opt.handler;
    this.area = opt.area;

    this.init();
    this.play();
}
ptp.prototype.init = function () {
    var self = this,
		  url = $(self.handler).find('li').eq(0).find("img").attr("src");

    $(self.handler).find('li').eq(0).addClass("active");

    $(self.area).attr("src", url);

    return self;
}
ptp.prototype.play = function () {
    var self = this;

    $(self.handler).find("li").each(function () {
        var _this = $(this),
				 url = _this.find("img").attr("src");
        _this.click(function () {
            $(self.handler).find("li").removeClass("active");
            _this.addClass("active");
            $(self.area).attr("src", url);
        });
    });

    return self;
}

/*分享*/
function share(opt) {
    var self = this;
    this.handler = opt.handler;
    this.title = "";
    this.url = "";

    this.init();
}
share.prototype.init = function () {
    var self = this,
		  classes = "";
    self.title = $('h1').text();
    self.url = encodeURIComponent(location);

    self.renren = "http://widget.renren.com/dialog/share?resourceUrl=" + self.url + "&srcUrl=" + self.url + "&title=" + self.title + "&pic=&description=";
    self.weibo = "http://v.t.sina.com.cn/share/share.php?title=" + self.title + "&url=" + self.url + "&source=bookmark&appkey=2992571369";
    self.db = "http://shuo.douban.com/!service/share?image=&href=" + self.url + "&name=" + self.title;
    self.tx = "http://share.v.t.qq.com/index.php?c=share&a=index&title=" + self.title + "&url=" + self.url + "&appkey=118cd1d635c44eab9a4840b2fbf8b0fb&pic=";
    self.kaixin = "http://www.kaixin001.com/rest/records.php?content=" + self.title + "&url=" + self.url + "&starid=&aid=100018706&style=11&pic=&t=55";
    $(self.handler).click(function (e) {
        e.preventDefault();
        _this = $(this);
        classes = _this.attr('class').split('-')[1];
        location.href = self[classes];
    });
    return self;
}
/*flash 播放器*/
function flashPlay(opt) {
    var self = this;
    this.id = opt.id;

    self.init();
}
flashPlay.prototype.init = function () {
    var self = this;
    _url = $('#' + self.id).attr("data-value");
    swfobject.embedSWF('/_assets/player/play.swf?source=' + _url, self.id, '368', '242', '9.0.0', null, null, { 'wmode': 'transparent' }, null, null);

    return this;
}

/*img弹出层*/
function imgPop(opt) {
    var self = this;
    self.handler = opt.handler;
    self.slt = opt.slt;
    self.init();
}
imgPop.prototype.init = function () {
    var self = this,
		  html = '',
		  lists = '<ul>',
		  docwidth = ($(window).width() - 694) / 2;

    for (var i = 0; i < $(self.slt).length; i++) {
        lists += '<li><img src="' + $(self.slt).eq(i).attr('src') + '"/></li>';
    }
    lists += "</ul>";

    html += '<div id="mask"></div><div id="popup"><span class="btn-close">X</span><div class="pop-content">' + lists + '</div><span class="btn-prev">Prev</span><span class="btn-next">Next</span></div>';

    $(self.handler).click(function () {
        window.scrollTo(0, 0);
        $('body').append(html);
        $("#popup").css({ 'left': docwidth + 'px' });

        new slider({
            dir: 'left',
            area: '#popup .pop-content ul',
            prevbtn: '#popup .btn-prev',
            nxtbtn: '#popup .btn-next'
        });

    });

    $('.btn-close').live('click', function () {
        $('#mask').remove();
        $('#popup').remove();
    });

    return self;
}


/*导航
$(function(){
$(".nav > ul > li > a").hover(function(){
$(this).addClass("active2");
$(this).siblings("ul").addClass("block2");
},function(){
$(this).removeClass("active2");
$(this).siblings("ul").removeClass("block2");
});
})
*/
$(function () {
    var myDate = new Date();
    _year = myDate.getFullYear();
    _years = "<ul>";
    for (var i = 102; i > 1; i--) {
        _years += '<li value=' + _year + '>' + _year + '</li>';
        _year--;
    }
    _years += "</ul>";
    $('.year').append(_years);


    var count = 1;
    var count2 = 1;

    $(".year").click(function () {
        var _this = $(this), _height = $(this).find("ul").height() + 24;
        $(this).children("ul").toggle();
        var self = this;
        if (count % 2 == 0) {
            _this.css({
                height: '24px'
            });
            count++;
        } else {
            _this.css({
                height: _height + 'px'
            });
            count++;
        }

        $(".year").find('li').live("click", function () {
            var _this = $(this);
            $(".year").find('span').html(_this.text()).attr("value", _this.attr("value"));
            $('input[name="year"]').val(_this.attr("value"));
        });
    })

    $(".month").click(function () {
        $(this).children("ul").toggle();
        var self = this;
        var _this = $(this),
			  _height = $(this).find("ul").height() + 24;
        if (count2 % 2 == 0) {
            _this.css({
                height: '24px'
            });
            count2++;
        } else {
            _this.css({
                height: _height + 'px'
            });
            count2++;
        }

        $(".month").find('li').live("click", function () {
            var _this = $(this);
            $(".month").find('span').html(_this.text()).attr("value", _this.attr("value"));
            $('input[name="month"]').val(_this.attr("value"));
        });
    })



    $(".login").click(function () {
        window.scrollTo(0, 0);
        var docwidth = ($(window).width() - 694) / 2;
        $("#login2").css({ 'left': docwidth + 'px' });
        $("#login2").show();
        $(".black_overlay").show();
        return false;
    });

    $(".demandNoLogin").click(function () {
        window.scrollTo(0, 0);
        var docwidth = ($(window).width() - 694) / 2;
        $("#login2").css({ 'left': docwidth + 'px' });
        $("#login2").show();
        $(".black_overlay").show();
        return false;
    });


    $(".close").click(function () {
        $("#login2").hide();
        $(".black_overlay").hide();
    });

    $(".jibenxinxianniu").click(function () {
        $(".jibenxinxi").toggle();
    });
})
